<?php
class UserService
{
	static private function _encodePassword($password, $passwordMask)
	{
		return md5(md5($password) . $passwordMask);
	}

	static public function authenticateUser($user)
	{
		$userDao = new UserDao();
		if ($userRow = $userDao->getUserByUsername($user['username']))
		{
			if ($userRow["password"] != self::_encodePassword($user["password"], $userRow["password_mask"]))
			{
				$result["code"] = 1003;
				$result["message"] = "密码不匹配";
			}
			else if ($userRow["disabled"])
			{
				$result["code"] = 1004;
				$result["message"] = "该用户已被禁止使用";
			}
			else
			{
				$result["code"] = 200;
				Cookie::setCookie(array("name" => "user_id", "value" => $userRow['id']));
				/*
				 * 为Discuz, Uchome设置cookie
				 */
				ComsenzUserService::setAuthCookie($userRow['id']);
			}
		}
		else
		{
			$result["code"] = 404;
			$result["message"] = "没有这个用户";
		}
		return $result;
	}

	static public function createUser($user)
	{
		$user = FormDataProcessor::standardize($user);
		$userDao = new UserDao();
		$ipBlacklistDao = new IpBlacklistDao();
		if ($ipBlacklistDao->getBlacklistByIp($user["signup_ip"]))
		{
			$result["code"] = 1103;
			$result["message"] = "您的IP已被禁止注册";
			return $result;
		}
		if ($userDao->getUserByUsername($user['username']))
		{
			$result["code"] = 1101;
			$result["message"] = "您使用的用户名已经被人注册了";
			return $result;
		}
		if ($userDao->getUserByEmail($user['email']))
		{
			$result["code"] = 1102;
			$result["message"] = "您使用的邮件地址已经被人注册了";
			return $result;
		}
		$user["password_mask"] = substr(md5(uniqid()),0,6);
		$user["password"] = self::_encodePassword($user["password"], $user["password_mask"]);
		if ($userId = $userDao->insertUser($user))
		{
			$result["code"] = 200;
			$result["data"] = array("user_id" => $userId);
			/*
			 * @for_comsenz 往Ucenter/Discuz/Uchome库中插入用户
			 */
			$comsenzUserDao = new ComsenzUserDao();
			$comsenzUserDao->insertUsers(array_merge($user, array("uid" => $userId)));
			Cookie::setCookie(array("name" => "user_id", "value" => $userId));
			ComsenzUserService::setAuthCookie($userId);
		}
		else
		{
			$result["code"] = 500;
			$result["message"] = "服务器内部错误";
		}
		return $result;
	}

	static public function updateUser($user)
	{
		$user = FormDataProcessor::standardize($user);
		$userDao = new UserDao();
		if (!$userDao->getUser($user['id']))
		{
			$result["code"] = 404;
			$result["message"] = "不存在这个用户[id=" . $user['id'] . "]";
		}
		else
		{
			if (isset($user["password"]) && strlen($user["password"]))
			{
				$userRow = $userDao->getUser($user["id"]);
				$user["password"] = self::_encodePassword($user["password"], $userRow["password_mask"]);
			}
			else
			{
				unset($user["password"]);
			}
			if ($userDao->updateUser($user))
			{
				$comsenzUserDao = new ComsenzUserDao();
				if (isset($user["password"]) || isset($user["email"]))
				{
					$comsenzUserDao->updateUsers($user);
				}
				$result["code"] = 200;
			}
			else
			{
				$result["code"] = 500;
				$result["message"] = "服务器内部错误";
			}
		}
		return $result;
	}
	
	static public function signoutUser()
	{
		Cookie::delCookie(array("name" => "user_id"));
		ComsenzUserService::delAuthCookie();
		$result["code"] = 200;
		return $result;
	}
	
	static public function searchUser($args)
	{
		$userDao = new UserDao();
		$condition["where"]["expression"] = "";
		if (isset($args["username"]) && strlen($args["username"]))
		{
			$condition["where"]["expression"] .= " AND username = :username";
			$condition["where"]["value"]["username"] = $args["username"];
		}
		if (isset($args["signup_ip"]) && strlen($args["signup_ip"]))
		{
			$condition["where"]["expression"] .= " AND signup_ip = :signup_ip";
			$condition["where"]["value"]["signup_ip"] = $args["signup_ip"];
		}
		if (strlen($condition["where"]["expression"]))
		{
			$condition["where"]["expression"] = substr($condition["where"]["expression"], 5);
			$users = $userDao->getUsers($condition);
		}
		else
		{
			$users = false;
		}
		return $users;
	}
}
